package cn.datax.service.data.market.controller;

import cn.datax.common.base.BaseController;
import cn.datax.common.core.JsonPage;
import cn.datax.common.core.R;
import cn.datax.common.utils.SecurityUtil;
import cn.datax.common.validate.ValidationGroups;
import cn.datax.service.data.market.api.entity.DataApiEntity;
import cn.datax.service.data.market.api.query.DataApiQuery;
import cn.datax.service.data.market.api.vo.DataApiVo;
import cn.datax.service.data.market.cata.dto.CataDto;
import cn.datax.service.data.market.cata.dto.CataResourceRelaDto;
import cn.datax.service.data.market.cata.dto.UserResourceInfoDto;
import cn.datax.service.data.market.cata.entity.CataEntity;
import cn.datax.service.data.market.cata.entity.ResourceInfoEntity;
import cn.datax.service.data.market.cata.entity.UserResourceInfoEntity;
import cn.datax.service.data.market.cata.query.UserResourceQuery;
import cn.datax.service.data.market.cata.vo.CataVo;
import cn.datax.service.data.market.cata.vo.ResourceInfoVo;
import cn.datax.service.data.market.cata.vo.UserResourceInfoVo;
import cn.datax.service.data.market.mapstruct.CataMapper;
import cn.datax.service.data.market.mapstruct.ResourceInfoMapper;
import cn.datax.service.data.market.mapstruct.UserResourceInfoMapper;
import cn.datax.service.data.market.service.CataResourceService;
import cn.datax.service.data.market.service.CataService;
import cn.datax.service.data.market.service.UserResourceService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuwei
 * @since 2019-09-04
 */
@Api(value="用户资源接口", tags = {"用户资源接口"})
@RestController
@RequestMapping("/userResource")
public class UserResourceController extends BaseController {

    @Autowired
    private UserResourceService userResourceService;

    @Autowired
    private UserResourceInfoMapper userResourceInfoMapper;


    @ApiOperation(value = "获取数据目录详细信息", notes = "根据url的id来获取数据目录详细信息")
    @ApiImplicitParam(name = "id", value = "部门ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{id}")
    public R getUserResourceById(@PathVariable String id) {
        UserResourceInfoEntity userResourceEntity = userResourceService.getById(id);
        return R.ok().setData(userResourceInfoMapper.toVO(userResourceEntity));
    }


    /**
     * 分页查询信息
     *
     * @param dataQuery
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dataApiQuery", value = "查询实体dataApiQuery", required = true, dataTypeClass = DataApiQuery.class)
    })
    @GetMapping("/page")
    public R getUserResourcePage(UserResourceQuery dataQuery) {
        QueryWrapper<UserResourceInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(dataQuery.getResourceName()), "resource_name", dataQuery.getResourceName());
        queryWrapper.like(StrUtil.isNotBlank(dataQuery.getResourceType()), "resource_type", dataQuery.getResourceType());
        IPage<UserResourceInfoEntity> page = userResourceService.pageGetUserResourceByUserId(new Page<>(dataQuery.getPageNum(), dataQuery.getPageSize()), null,dataQuery.getResourceType(),dataQuery.getResourceName());
        if(null != page) {
            List<UserResourceInfoVo> collect = page.getRecords().stream().map(userResourceInfoMapper::toVO).collect(Collectors.toList());
            JsonPage<UserResourceInfoVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
            return R.ok().setData(jsonPage);
        }else{
            return R.ok().setData(new JsonPage<UserResourceInfoVo>(1l, 1l, 0l, new ArrayList<UserResourceInfoVo>()));
        }
    }

    @ApiOperation(value = "获取目录列表", notes = "")
    @GetMapping("/list")
    public R getUserResourceList() {
        List<UserResourceInfoEntity> list = userResourceService.getUserResourceByUserId(null,"api");
        List<UserResourceInfoVo> collect = list.stream().map(userResourceInfoMapper::toVO).collect(Collectors.toList());
        return R.ok().setData(collect);
    }

    @ApiOperation(value = "创建", notes = "创建目录")
    @ApiImplicitParam(name = "cata", value = "目录详细实体cata", required = true, dataType = "CataDto")
    @PostMapping()
    public R saveData(@RequestBody @Validated({ValidationGroups.Insert.class}) UserResourceInfoDto data) {
        data.setUserId(SecurityUtil.getUserId());
        return R.ok().setData(userResourceService.saveData(data));
    }
    /**
     * 申请资源提交审核
     * @param id
     * @return
     */
    @PostMapping(value = "/{id}/applySubmit")
    public R releaseDataResourceSubmit(@PathVariable String id){
        userResourceService.applySubmit(id);
        return R.ok();
    }

    /**
     *
     * @param id
     * @param data
     * @return
     */
    @PutMapping("/{id}")
    public R updateUserResource(@PathVariable String id, @RequestBody @Validated({ValidationGroups.Update.class}) UserResourceInfoDto data) {
        userResourceService.updateData(data);
        return R.ok();
    }


    /**
     * 下载资源数据
     * @param id
     * @return
     */
    @PostMapping(value = "/download/{id}")
    public R downloadUserResource(@PathVariable String id){
        //根据ID获取到资源信息，取出数据源、表、字段相关信息  maket_table.config_json字段存储JSON数据
        //通过JSON-PATH 提取相关信息
        //拼装select 语句
        // 连接数据源
        // 执行SQL
        // 生成excel
        return R.ok();
    }

}

